<!DOCTYPE html>
<html>

<head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>百度地图有道翻译</title>
    <style type="text/css">
        html {
            height: 100%
        }

        body {
            height: 100%;
            margin: 0px;
            padding: 0px
        }

        #container {
            height: 100%
        }
    </style>
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=gVako2Nm7CtU0EO7YivNmjpmW6wnSMWX">
        //v2.0版本的引用方式：src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"
    </script>
    <script src="https://cdn.bootcss.com/blueimp-md5/2.10.0/js/md5.js"></script>
</head>

<body>
    <div id="container"></div>
    <div style="position: absolute;top: 10px;right: 50px;">
        <div>
            <span>具体地址:</span>
            <span id="str"></span>
        </div>
        <br/>
        <div>
            <span> 翻译内容:</span>
            <span id="ystr"></span>
        </div>
        <input type="hidden" id="add" />
        <br />
        <div>
            <span>译后内容:</span>
            <span id="hstr"></span>
        </div>
        <br />
        <button class="layui-btn" onclick="fanyi('zh-CHS')">中文</button>
        <button class="layui-btn" onclick="fanyi('EN')">英文</button>
        <button class="layui-btn" onclick="fanyi('ja')">日语</button>
        <button class="layui-btn" onclick="fanyi('ko')">韩语</button>
    </div>
    <script type="text/javascript">
        var map = new BMap.Map("container");
        // 创建地图实例  
        var point = new BMap.Point(116.404, 39.915);
        // 创建点坐标  
        map.centerAndZoom(point, 5);
        map.enableScrollWheelZoom(true);
        var marker = new BMap.Marker(point); // 创建标注    
        map.addOverlay(marker);
        marker.enableDragging();
        marker.addEventListener("dragend", function (e) {

            var myGeo = new BMap.Geocoder();

            $.ajax({
                type: "get",
                dataType: 'jsonp',
                url: "http://api.map.baidu.com/geocoder/v2/?location=" + e.point.lat + "," + e.point.lng +
                    "&output=json&pois=1&ak=gVako2Nm7CtU0EO7YivNmjpmW6wnSMWX",
                success: function (res) {
                    console.log(res);
                    console.log(res.status);
                    console.log(res.result);
                    //alert(res.result.formatted_address);
                    $("#str").text(res.result.formatted_address);
                    $("#ystr").text(res.result.addressComponent.country + "\n" + res.result.addressComponent
                        .province + "\n" + res.result.addressComponent.city + "\n")
                    $("#add").val(res.result.addressComponent.country + "\n" + res.result.addressComponent
                        .province + "\n" + res.result.addressComponent.city + "\n")
                }
            });

        })



        // 初始化地图，设置中心点坐标和地图级别
    </script>


    <script type="text/javascript">
        function fanyi(yuyan) {


            var appKey = '2706a65acf8337b9';
            var key = 'DM3Z7IMR7YjvSbL8u94hNwh8gUJnSDEj'; //注意：暴露appSecret，有被盗用造成损失的风险
            var salt = (new Date).getTime();
            var query = $("#add").val(); //'apple\norange\nbanana\npear';
            // 多个query可以用\n连接  如 query='apple\norange\nbanana\npear'
            var from = 'auto';
            var to = yuyan;
            var str1 = appKey + query + salt + key;
            var sign = md5(str1);
            $.ajax({
                url: 'http://openapi.youdao.com/api',
                type: 'post',
                dataType: 'jsonp',
                data: {
                    q: query,
                    appKey: appKey,
                    salt: salt,
                    from: from,
                    to: to,
                    sign: sign
                },
                success: function (data) {
console.log(data);
                    if (data.errorCode == "0") {
                        $("#hstr").text(data.translation[0]);
                    }
                }
            });
        }
    </script>

</body>

</html>